package com.education.module.message;

import java.util.List;

import javax.servlet.ServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.education.domain.Message;
import com.education.domain.User;
import com.education.framework.base.BaseController;
import com.education.framework.domain.SearchParams;
import com.education.framework.page.Page;
import com.education.framework.session.SessionHelper;

@Controller
@RequestMapping("message")
public class MessageController extends BaseController{

	@Autowired
	private MessageServices services;
	
	@RequestMapping(value = "")
	public String list(Model model, SearchParams searchParams,Page page,ServletRequest request){
		List<Message> list = services.find(searchParams,page);
		model.addAttribute("list", list);
		model.addAttribute("page", page);
		model.addAttribute("searchParams", searchParams);
		return "/message/messageList";
	}
	
	@RequestMapping(value = "create", method = RequestMethod.GET)
	public String createForm(Model model) {
		Message message = new Message();
		model.addAttribute("message", message);
		model.addAttribute("action","create");
		return "/message/messageEdit";
	}

	@RequestMapping(value = "create", method = RequestMethod.POST)
	public String create(Message message, Model model,RedirectAttributes redirectAttributes) {
		User sessionUser = SessionHelper.getInstance().getUser();
		message.setCreateUser(sessionUser);
		message.setUpdateUser(sessionUser);
		services.save(message);
		redirectAttributes.addFlashAttribute("message", "SUCCESS");
		return "redirect:/message?map['msgType']=" + message.getMsgType();
	}

	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	public String updateForm(@PathVariable("id") Integer id, Model model) {
		Message message = services.findForObject(id);
		model.addAttribute("message", message);
		model.addAttribute("action", "update");
		return "/message/messageEdit";
	}

	@RequestMapping(value = "update", method = RequestMethod.POST)
	public String update(Message message, RedirectAttributes redirectAttributes) {
		User sessionUser = SessionHelper.getInstance().getUser();
		message.setUpdateUser(sessionUser);
		services.update(message);
		redirectAttributes.addFlashAttribute("message", "SUCCESS");
		return "redirect:/message?map['msgType']=" + message.getMsgType();
	}
	
	@RequestMapping(value = "delete/{id}")
	public String delete(@PathVariable("id") Integer id,RedirectAttributes redirectAttributes) {
		services.delete(id);
		redirectAttributes.addFlashAttribute("message", "SUCCESS");
		return "redirect:/message";
	}
	
	@RequestMapping(value = "updateObjectForm/{id}", method = RequestMethod.GET)
	public String updateObjectForm(@PathVariable("id") Integer id, Model model) {
		if(services.findCount(String.valueOf(id)) == 0){
			Message message = new Message();
			message.setId(id);
			model.addAttribute("message", message);
		}else{
			Message message = services.findForObject(id);
			model.addAttribute("message", message);
		}
		model.addAttribute("action", "updateObject");
		return "/message/messageEdit";
	}
	
	@RequestMapping(value = "updateObject", method = RequestMethod.POST)
	public String updateObject(Message message, RedirectAttributes redirectAttributes) {
		services.delete(message.getId());
		
		User sessionUser = SessionHelper.getInstance().getUser();
		message.setCreateUser(sessionUser);
		message.setUpdateUser(sessionUser);
		services.save(message);
		redirectAttributes.addFlashAttribute("message", "SUCCESS");
		return "redirect:/message/updateObjectForm/" + message.getId() + "/" + message.getMsgType();
	}
}
